﻿using Sunny.UI;
using LogLevel = NLog.LogLevel;

namespace WinFormsSprayProcessSCADASystem.Extension
{
    public static class FormExtension
    {
        public static void ShowWarning(this Form form, string title)
        {
            try
            {
                Point middlePoint = CalculateMiddlePoint(form);
                UIMessageTip.ShowWarning(title, 1000, false, middlePoint);
            }
            catch (Exception ex)
            {
                Globals.NLogger.Log(LogLevel.Error, ex.Message);
            }
        }

        public static void ShowOk(this Form form, string title)
        {
            try
            {
                Point middlePoint = CalculateMiddlePoint(form);
                UIMessageTip.ShowOk(title, 1000, false, middlePoint);
            }
            catch (Exception ex)
            {
                Globals.NLogger.Log(LogLevel.Error, ex.Message);
            }
        }

        public static void ShowError(this Form form, string title)
        {
            try
            {
                Point middlePoint = CalculateMiddlePoint(form);
                UIMessageTip.ShowError(title, 1000, false, middlePoint);
            }
            catch (Exception ex)
            {
                Globals.NLogger.Log(LogLevel.Error, ex.Message);
            }
        }
        #region 获取所有控件
        // 获取指定页面的所有控件
        public static List<Control> GetAllControls(this UIPage page)
        {
            List<Control> allControls = new List<Control>();
            CollectControls(page.Controls, allControls);
            return allControls;
        }

        // 递归方法，用于收集控件
        private static void CollectControls(Control.ControlCollection controls, List<Control> allControls)
        {
            foreach (Control control in controls)
            {
                allControls.Add(control); // 将当前控件添加到列表中
                                          // 如果当前控件有子控件，则递归调用此方法
                if (control.HasChildren)
                {
                    CollectControls(control.Controls, allControls);
                }
            }
        }
        #endregion
        // 提取计算中间点坐标的方法
        private static Point CalculateMiddlePoint(Form form)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form), "传入的窗体不能为空。");
            }

            try
            {
                // 获取当前窗口在屏幕上的位置
                Point screenLocation = form.PointToScreen(Point.Empty);

                // 获取当前窗口的宽度和高度
                int width = form.Width;
                int height = form.Height;

                // 计算在屏幕坐标系下窗口中间的坐标
                int x = screenLocation.X + width / 2;
                int y = screenLocation.Y + height / 5;

                // 创建Point对象表示中间点
                return new Point(x, y);
            }
            catch (Exception ex)
            {
                // 处理异常，这里简单输出异常信息，实际使用时可以根据需求进行更详细的处理
                Console.WriteLine($"计算中间点坐标时出现异常: {ex.Message}");
                return Point.Empty;
            }
        }

        public static void LogError(this Form form, string message)
        {

            Globals.NLogger.Log(LogLevel.Error, message);
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
            Globals.Logger.CreateLogAsync(new BLL.DTO.LogDto.LogAddDto()
            {
                Content = message,
                Level = 3
            });
#pragma warning restore CS4014 
        }

        public static void LogMes(this Form form, string message)
        {
            //Globals.NLogger.Log(LogLevel.Info, message);
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
            Globals.Logger.CreateLogAsync(new BLL.DTO.LogDto.LogAddDto()
            {
                Content = message,
                Level = 1
            });
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
        }

        public static void ActionLogMes(this Form form, string message)
        {
            //Globals.NLogger.Log(LogLevel.Info, $"{Globals.CurrentUser.UserName}({Globals.CurrentUser.Id}):{message}");
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
            Globals.Logger.CreateLogAsync(new BLL.DTO.LogDto.LogAddDto()
            {
                Content = $"{Globals.CurrentUser.UserName}({Globals.CurrentUser.Id}):{message}",
                Level = 1
            });
#pragma warning restore CS4014 
        }

        public static void LogWarn(this Form form, string message)
        {
            //Globals.NLogger.Log(LogLevel.Warn, message);
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
            Globals.Logger.CreateLogAsync(new BLL.DTO.LogDto.LogAddDto()
            {
                Content = message,
                Level = 2
            });
#pragma warning restore CS4014 
        }
    }
}
